package org.jmat.batch.tasklet;

import java.io.File;
import java.util.Collection;
import java.util.LinkedHashSet;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.core.io.Resource;

public class CreateFlatFileTasklet implements Tasklet {

	private final Log log = LogFactory.getLog(CreateFlatFileTasklet.class);
	private Resource directoryFiles;	
	
	public Resource getDirectoryFiles() {
		return directoryFiles;
	}

	public void setDirectoryFiles(Resource directoryFiles) {
		this.directoryFiles = directoryFiles;
	}
	
	public ExitStatus execute() throws Exception {
		File file = new File(directoryFiles.getFile().getAbsoluteFile() + "/person.txt");
		Collection<String> lines = new LinkedHashSet<String>();
		StringBuilder str = new StringBuilder();
		for (int i = 1; i <= 10000; i++){
			str.append(i + ",Name_" + i + ",firestname_" + i + ",1983-01-06");
			lines.add(str.toString());
			str.delete(0, str.length());
		}
		
		if (log.isDebugEnabled()){
			log.debug("lines : " + lines.size());
		}
		FileUtils.writeLines(file, lines);
		
		return ExitStatus.FINISHED;
	}

}
